package sse.ustc.cc.util;

import android.annotation.SuppressLint;
import android.provider.ContactsContract.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

@SuppressLint("SimpleDateFormat")
public class DataString {
	private String mYear;
	private String mMonth;
	private String mDay;
	private String mWay;
	private static String mWeek;
	private static String year;
	private static String month;
	private static String day;
	static SimpleDateFormat sMonth;
	static SimpleDateFormat sDay;
	static SimpleDateFormat sYear;

	public String StringData() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));// 获取周几
		if ("1".equals(mWay)) {
			mWay = "7";
		} else if ("2".equals(mWay)) {
			mWay = "1";
		} else if ("3".equals(mWay)) {
			mWay = "2";
		} else if ("4".equals(mWay)) {
			mWay = "3";
		} else if ("5".equals(mWay)) {
			mWay = "4";
		} else if ("6".equals(mWay)) {
			mWay = "5";
		} else if ("7".equals(mWay)) {
			mWay = "6";
		}
		return mWay;
	}

	// 获取当前时间所在年的周数
	public static int getWeekOfYear(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setMinimalDaysInFirstWeek(7);
		c.setTime(date);
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	// 获取当前时间所在年的最大周数
	public static int getMaxWeekNumOfYear(int year) {
		Calendar c = new GregorianCalendar();
		c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);//设置成当年最后的一天2014.12.31.23:59:59
		
		return getWeekOfYear(c.getTime());
	}

	/**
	 * 产生周序列,即得到当前时间所在的年度是第几周
	 * 
	 * @return
	 */
	public static int getSeqWeek(Date date) {

		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cl = Calendar.getInstance(Locale.CHINA);
		// cl.setTime(sdf.parse("2014-12-31"));
		cl.setTime(date);
		int week = cl.get(Calendar.WEEK_OF_YEAR);
		// System.out.println("week" + week);
		cl.add(Calendar.DAY_OF_MONTH, -7);
		int year = cl.get(Calendar.YEAR);
		if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
			year += 1;
		}
		System.out.println(year + "年第" + week + "周");
		return week;
	}

	@SuppressLint("SimpleDateFormat")
	/**
	 * 获取当前时间处于课程设置里面的第几周
	 * 
	 * @return
	 */
	public static String getWeek(Date date) {
		sYear = new SimpleDateFormat("yyyy");// 年
		sMonth = new SimpleDateFormat("MM");// 月
		sDay = new SimpleDateFormat("dd");// 日
		year = sYear.format(date);
		month = sMonth.format(date);
		day = sDay.format(date);
		System.out.println("year" + year);
		System.out.println("month" + month);
		int currentWeek = 10;// 当前周数；
		int week = 0;
		String registerWeek = "49";// 设置当前周数时记录的当天所属日期的周数
		// String registerDay = "12";// 设置当前周数时记录的当天所属的日份
		String fromWeekDay = "0";// 设置中每周的开始日期？星期一：1，星期天：0

		if (getSeqWeek(date) == 1 && month.equals("12")) {// 表示当前周数处于年末
			week = currentWeek + getMaxWeekNumOfYear(Integer.parseInt(year))
					- Integer.parseInt(registerWeek) + getSeqWeek(date);
			System.out.println("week" + week);
		} else if (getSeqWeek(date) == 1 && month.equals("01")) {// 表示当前周数处于年初
			week = currentWeek
					+ getMaxWeekNumOfYear(Integer.parseInt(year) - 1)
					- Integer.parseInt(registerWeek) + getSeqWeek(date);
		} else if ((getSeqWeek(date) - Integer.parseInt(registerWeek)) < 0) {//表示已经是新的一年，如2015
			week = currentWeek
					+ getMaxWeekNumOfYear(Integer.parseInt(year) - 1)
					- Integer.parseInt(registerWeek) + getSeqWeek(date);
		}else{//表示处于当年，如2014
			week = currentWeek + (getSeqWeek(date))
					- Integer.parseInt(registerWeek);
		}

		mWeek = week + "";
		return mWeek;

	}

	/**
	 * 获取当前日期是星期几<br>
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static int getWeekOfDate(Date dt) {
		//String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return w;
	}

	public static void main(String args[]) {
		Date date = new Date();
		// System.out.println(getSeqWeek(date)); // 201451
		System.out.println(getWeek(date)); //
		// System.out.println(getWeekOfDate(date)); //
		System.out.println(getMaxWeekNumOfYear(2014)); //
		return;
	}
}
